<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>迭代器</title>
</head>
<body>
    <script type="text/javascript">
        var agg = (function() {
            var data = [1, 2, 3, 4, 5], index = 0, len = data.length;
            return {
                next: function() {
                    if(!this.hasNext()) {
                        return null;
                    }
                    return data[index++];
                },
                hasNext: function() {
                    return index < len;
                },
                rewind: function() {
                    index = 0;
                },
                current: function() {
                    var element = null;
                    if(index === 0){
                        element = data[0];
                    }else{
                        element = data[--index];
                    }
                    index++;
                    return element;
                }

            };
        }());

        while(agg.hasNext()) {
            console.log(agg.next());
        }
        console.log(agg.current());
    </script>
</body>
</html>